home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb-4.5 / dist / gdb / blockframe.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-29  |  16.8 KB  |  663 lines

  1. /* Get info from stack frames;
  2.    convert between frames, blocks, functions and pc values.
  3.    Copyright 1986, 1987, 1988, 1989, 1991 Free Software Foundation, Inc.
  4.  
  5. This file is part of GDB.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. #include "defs.h"
  22. #include "symtab.h"
  23. #include "bfd.h"
  24. #include "symfile.h"
  25. #include "objfiles.h"
  26. #include "frame.h"
  27. #include "gdbcore.h"
  28. #include "value.h"        /* for read_register */
  29. #include "target.h"        /* for target_has_stack */
  30. #include "inferior.h"        /* for read_pc */
  31.  
  32. /* Is ADDR inside the startup file?  Note that if your machine
  33.    has a way to detect the bottom of the stack, there is no need
  34.    to call this function from FRAME_CHAIN_VALID; the reason for
  35.    doing so is that some machines have no way of detecting bottom
  36.    of stack.  */
  37.  
  38. int
  39. inside_entry_file (addr)
  40.      CORE_ADDR addr;
  41. {
  42.   return (addr >= symfile_objfile -> ei.entry_file_lowpc &&
  43.       addr <  symfile_objfile -> ei.entry_file_highpc);
  44. }
  45.  
  46. /* Test a specified PC value to see if it is in the range of addresses
  47.    that correspond to the main() function.  See comments above for why
  48.    we might want to do this.
  49.  
  50.    Typically called from FRAME_CHAIN_VALID. */
  51.  
  52. int
  53. inside_main_func (pc)
  54. CORE_ADDR pc;
  55. {
  56.   return (symfile_objfile -> ei.main_func_lowpc  <= pc &&
  57.       symfile_objfile -> ei.main_func_highpc > pc);
  58. }
  59.  
  60. /* Test a specified PC value to see if it is in the range of addresses
  61.    that correspond to the process entry point function.  See comments
  62.    in objfiles.h for why we might want to do this.
  63.  
  64.    Typically called from FRAME_CHAIN_VALID. */
  65.  
  66. int
  67. inside_entry_func (pc)
  68. CORE_ADDR pc;
  69. {
  70.   return (symfile_objfile -> ei.entry_func_lowpc  <= pc &&
  71.       symfile_objfile -> ei.entry_func_highpc > pc);
  72. }
  73.  
  74. /* Address of innermost stack frame (contents of FP register) */
  75.  
  76. static FRAME current_frame;
  77.  
  78. /*
  79.  * Cache for frame addresses already read by gdb.  Valid only while
  80.  * inferior is stopped.  Control variables for the frame cache should
  81.  * be local to this module.
  82.  */
  83. struct obstack frame_cache_obstack;
  84.  
  85. /* Return the innermost (currently executing) stack frame.  */
  86.  
  87. FRAME
  88. get_current_frame ()
  89. {
  90.   /* We assume its address is kept in a general register;
  91.      param.h says which register.  */
  92.  
  93.   return current_frame;
  94. }
  95.  
  96. void
  97. set_current_frame (frame)
  98.      FRAME frame;
  99. {
  100.   current_frame = frame;
  101. }
  102.  
  103. FRAME
  104. create_new_frame (addr, pc)
  105.      FRAME_ADDR addr;
  106.      CORE_ADDR pc;
  107. {
  108.   struct frame_info *fci;    /* Same type as FRAME */
  109.  
  110.   fci = (struct frame_info *)
  111.     obstack_alloc (&frame_cache_obstack,
  112.            sizeof (struct frame_info));
  113.  
  114.   /* Arbitrary frame */
  115.   fci->next = (struct frame_info *) 0;
  116.   fci->prev = (struct frame_info *) 0;
  117.   fci->frame = addr;
  118.   fci->next_frame = 0;        /* Since arbitrary */
  119.   fci->pc = pc;
  120.  
  121. #ifdef INIT_EXTRA_FRAME_INFO
  122.   INIT_EXTRA_FRAME_INFO (0, fci);
  123. #endif
  124.  
  125.   return fci;
  126. }
  127.  
  128. /* Return the frame that called FRAME.
  129.    If FRAME is the original frame (it has no caller), return 0.  */
  130.  
  131. FRAME
  132. get_prev_frame (frame)
  133.      FRAME frame;
  134. {
  135.   /* We're allowed to know that FRAME and "struct frame_info *" are
  136.      the same */
  137.   return get_prev_frame_info (frame);
  138. }
  139.  
  140. /* Return the frame that FRAME calls (0 if FRAME is the innermost
  141.    frame).  */
  142.  
  143. FRAME
  144. get_next_frame (frame)
  145.      FRAME frame;
  146. {
  147.   /* We're allowed to know that FRAME and "struct frame_info *" are
  148.      the same */
  149.   return frame->next;
  150. }
  151.  
  152. /*
  153.  * Flush the entire frame cache.
  154.  */
  155. void
  156. flush_cached_frames ()
  157. {
  158.   /* Since we can't really be sure what the first object allocated was */
  159.   obstack_free (&frame_cache_obstack, 0);
  160.   obstack_init (&frame_cache_obstack);
  161.  
  162.   current_frame = (struct frame_info *) 0; /* Invalidate cache */
  163. }
  164.  
  165. /* Flush the frame cache, and start a new one if necessary.  */
  166. void
  167. reinit_frame_cache ()
  168. {
  169.   FRAME fr = current_frame;
  170.   flush_cached_frames ();
  171.   if (fr)
  172.     set_current_frame ( create_new_frame (read_register (FP_REGNUM),
  173.                       read_pc ()));
  174. }
  175.  
  176. /* Return a structure containing various interesting information
  177.    about a specified stack frame.  */
  178. /* How do I justify including this function?  Well, the FRAME
  179.    identifier format has gone through several changes recently, and
  180.    it's not completely inconceivable that it could happen again.  If
  181.    it does, have this routine around will help */
  182.  
  183. struct frame_info *
  184. get_frame_info (frame)
  185.      FRAME frame;
  186. {
  187.   return frame;
  188. }
  189.  
  190. /* If a machine allows frameless functions, it should define a macro
  191.    FRAMELESS_FUNCTION_INVOCATION(FI, FRAMELESS) in param.h.  FI is the struct
  192.    frame_info for the frame, and FRAMELESS should be set to nonzero
  193.    if it represents a frameless function invocation.  */
  194.  
  195. /* Return nonzero if the function for this frame has a prologue.  Many
  196.    machines can define FRAMELESS_FUNCTION_INVOCATION to just call this
  197.    function.  */
  198.  
  199. int
  200. frameless_look_for_prologue (frame)
  201.      FRAME frame;
  202. {
  203.   CORE_ADDR func_start, after_prologue;
  204.   func_start = (get_pc_function_start (frame->pc) +
  205.         FUNCTION_START_OFFSET);
  206.   if (func_start)
  207.     {
  208.       after_prologue = func_start;
  209. #ifdef SKIP_PROLOGUE_FRAMELESS_P
  210.       /* This is faster, since only care whether there *is* a prologue,
  211.      not how long it is.  */
  212.       SKIP_PROLOGUE_FRAMELESS_P (after_prologue);
  213. #else
  214.       SKIP_PROLOGUE (after_prologue);
  215. #endif
  216.       return after_prologue == func_start;
  217.     }
  218.   else
  219.     /* If we can't find the start of the function, we don't really
  220.        know whether the function is frameless, but we should be able
  221.        to get a reasonable (i.e. best we can do under the
  222.        circumstances) backtrace by saying that it isn't.  */
  223.     return 0;
  224. }
  225.  
  226. /* Default a few macros that people seldom redefine.  */
  227.  
  228. #if !defined (INIT_FRAME_PC)
  229. #define INIT_FRAME_PC(fromleaf, prev) \
  230.   prev->pc = (fromleaf ? SAVED_PC_AFTER_CALL (prev->next) : \
  231.           prev->next ? FRAME_SAVED_PC (prev->next) : read_pc ());
  232. #endif
  233.  
  234. #ifndef FRAME_CHAIN_COMBINE
  235. #define    FRAME_CHAIN_COMBINE(chain, thisframe) (chain)
  236. #endif
  237.  
  238. /* Return a structure containing various interesting information
  239.    about the frame that called NEXT_FRAME.  Returns NULL
  240.    if there is no such frame.  */
  241.  
  242. struct frame_info *
  243. get_prev_frame_info (next_frame)
  244.      FRAME next_frame;
  245. {
  246.   FRAME_ADDR address;
  247.   struct frame_info *prev;
  248.   int fromleaf = 0;
  249.  
  250.   /* If the requested entry is in the cache, return it.
  251.      Otherwise, figure out what the address should be for the entry
  252.      we're about to add to the cache. */
  253.  
  254.   if (!next_frame)
  255.     {
  256.       if (!current_frame)
  257.     {
  258.       error ("You haven't set up a process's stack to examine.");
  259.     }
  260.  
  261.       return current_frame;
  262.     }
  263.  
  264.   /* If we have the prev one, return it */
  265.   if (next_frame->prev)
  266.     return next_frame->prev;
  267.  
  268.   /* On some machines it is possible to call a function without
  269.      setting up a stack frame for it.  On these machines, we
  270.      define this macro to take two args; a frameinfo pointer
  271.      identifying a frame and a variable to set or clear if it is
  272.      or isn't leafless.  */
  273. #ifdef FRAMELESS_FUNCTION_INVOCATION
  274.   /* Still don't want to worry about this except on the innermost
  275.      frame.  This macro will set FROMLEAF if NEXT_FRAME is a
  276.      frameless function invocation.  */
  277.   if (!(next_frame->next))
  278.     {
  279.       FRAMELESS_FUNCTION_INVOCATION (next_frame, fromleaf);
  280.       if (fromleaf)
  281.     address = next_frame->frame;
  282.     }
  283. #endif
  284.  
  285.   if (!fromleaf)
  286.     {
  287.       /* Two macros defined in tm.h specify the machine-dependent
  288.      actions to be performed here.
  289.      First, get the frame's chain-pointer.
  290.      If that is zero, the frame is the outermost frame or a leaf
  291.      called by the outermost frame.  This means that if start
  292.      calls main without a frame, we'll return 0 (which is fine
  293.      anyway).
  294.  
  295.      Nope; there's a problem.  This also returns when the current
  296.      routine is a leaf of main.  This is unacceptable.  We move
  297.      this to after the ffi test; I'd rather have backtraces from
  298.      start go curfluy than have an abort called from main not show
  299.      main.  */
  300.       address = FRAME_CHAIN (next_frame);
  301.       if (!FRAME_CHAIN_VALID (address, next_frame))
  302.     return 0;
  303.       address = FRAME_CHAIN_COMBINE (address, next_frame);
  304.     }
  305.   if (address == 0)
  306.     return 0;
  307.  
  308.   prev = (struct frame_info *)
  309.     obstack_alloc (&frame_cache_obstack,
  310.            sizeof (struct frame_info));
  311.  
  312.   if (next_frame)
  313.     next_frame->prev = prev;
  314.   prev->next = next_frame;
  315.   prev->prev = (struct frame_info *) 0;
  316.   prev->frame = address;
  317.   prev->next_frame = prev->next ? prev->next->frame : 0;
  318.  
  319. #ifdef INIT_EXTRA_FRAME_INFO
  320.   INIT_EXTRA_FRAME_INFO(fromleaf, prev);
  321. #endif
  322.  
  323.   /* This entry is in the frame queue now, which is good since
  324.      FRAME_SAVED_PC may use that queue to figure out it's value
  325.      (see tm-sparc.h).  We want the pc saved in the inferior frame. */
  326.   INIT_FRAME_PC(fromleaf, prev);
  327.  
  328.   return prev;
  329. }
  330.  
  331. CORE_ADDR
  332. get_frame_pc (frame)
  333.      FRAME frame;
  334. {
  335.   struct frame_info *fi;
  336.   fi = get_frame_info (frame);
  337.   return fi->pc;
  338. }
  339.  
  340. #if defined (FRAME_FIND_SAVED_REGS)
  341. /* Find the addresses in which registers are saved in FRAME.  */
  342.  
  343. void
  344. get_frame_saved_regs (frame_info_addr, saved_regs_addr)
  345.      struct frame_info *frame_info_addr;
  346.      struct frame_saved_regs *saved_regs_addr;
  347. {
  348.   FRAME_FIND_SAVED_REGS (frame_info_addr, *saved_regs_addr);
  349. }
  350. #endif
  351.  
  352. /* Return the innermost lexical block in execution
  353.    in a specified stack frame.  The frame address is assumed valid.  */
  354.  
  355. struct block *
  356. get_frame_block (frame)
  357.      FRAME frame;
  358. {
  359.   struct frame_info *fi;
  360.   CORE_ADDR pc;
  361.  
  362.   fi = get_frame_info (frame);
  363.  
  364.   pc = fi->pc;
  365.   if (fi->next_frame != 0)
  366.     /* We are not in the innermost frame.  We need to subtract one to
  367.        get the correct block, in case the call instruction was the
  368.        last instruction of the block.  If there are any machines on
  369.        which the saved pc does not point to after the call insn, we
  370.        probably want to make fi->pc point after the call insn anyway.  */
  371.     --pc;
  372.   return block_for_pc (pc);
  373. }
  374.  
  375. struct block *
  376. get_current_block ()
  377. {
  378.   return block_for_pc (read_pc ());
  379. }
  380.  
  381. CORE_ADDR
  382. get_pc_function_start (pc)
  383.      CORE_ADDR pc;
  384. {
  385.   register struct block *bl;
  386.   register struct symbol *symbol;
  387.   register struct minimal_symbol *msymbol;
  388.   CORE_ADDR fstart;
  389.  
  390.   if ((bl = block_for_pc (pc)) != NULL &&
  391.       (symbol = block_function (bl)) != NULL)
  392.     {
  393.       bl = SYMBOL_BLOCK_VALUE (symbol);
  394.       fstart = BLOCK_START (bl);
  395.     }
  396.   else if ((msymbol = lookup_minimal_symbol_by_pc (pc)) != NULL)
  397.     {
  398.       fstart = msymbol -> address;
  399.     }
  400.   else
  401.     {
  402.       fstart = 0;
  403.     }
  404.   return (fstart);
  405. }
  406.  
  407. /* Return the symbol for the function executing in frame FRAME.  */
  408.  
  409. struct symbol *
  410. get_frame_function (frame)
  411.      FRAME frame;
  412. {
  413.   register struct block *bl = get_frame_block (frame);
  414.   if (bl == 0)
  415.     return 0;
  416.   return block_function (bl);
  417. }
  418.  
  419. /* Return the blockvector immediately containing the innermost lexical block
  420.    containing the specified pc value, or 0 if there is none.
  421.    PINDEX is a pointer to the index value of the block.  If PINDEX
  422.    is NULL, we don't pass this information back to the caller.  */
  423.  
  424. struct blockvector *
  425. blockvector_for_pc (pc, pindex)
  426.      register CORE_ADDR pc;
  427.      int *pindex;
  428. {
  429.   register struct block *b;
  430.   register int bot, top, half;
  431.   register struct symtab *s;
  432.   struct blockvector *bl;
  433.  
  434.   /* First search all symtabs for one whose file contains our pc */
  435.   s = find_pc_symtab (pc);
  436.   if (s == 0)
  437.     return 0;
  438.  
  439.   bl = BLOCKVECTOR (s);
  440.   b = BLOCKVECTOR_BLOCK (bl, 0);
  441.  
  442.   /* Then search that symtab for the smallest block that wins.  */
  443.   /* Use binary search to find the last block that starts before PC.  */
  444.  
  445.   bot = 0;
  446.   top = BLOCKVECTOR_NBLOCKS (bl);
  447.  
  448.   while (top - bot > 1)
  449.     {
  450.       half = (top - bot + 1) >> 1;
  451.       b = BLOCKVECTOR_BLOCK (bl, bot + half);
  452.       if (BLOCK_START (b) <= pc)
  453.     bot += half;
  454.       else
  455.     top = bot + half;
  456.     }
  457.  
  458.   /* Now search backward for a block that ends after PC.  */
  459.  
  460.   while (bot >= 0)
  461.     {
  462.       b = BLOCKVECTOR_BLOCK (bl, bot);
  463.       if (BLOCK_END (b) > pc)
  464.     {
  465.       if (pindex)
  466.         *pindex = bot;
  467.       return bl;
  468.     }
  469.       bot--;
  470.     }
  471.  
  472.   return 0;
  473. }
  474.  
  475. /* Return the innermost lexical block containing the specified pc value,
  476.    or 0 if there is none.  */
  477.  
  478. struct block *
  479. block_for_pc (pc)
  480.      register CORE_ADDR pc;
  481. {
  482.   register struct blockvector *bl;
  483.   int index;
  484.  
  485.   bl = blockvector_for_pc (pc, &index);
  486.   if (bl)
  487.     return BLOCKVECTOR_BLOCK (bl, index);
  488.   return 0;
  489. }
  490.  
  491. /* Return the function containing pc value PC.
  492.    Returns 0 if function is not known.  */
  493.  
  494. struct symbol *
  495. find_pc_function (pc)
  496.      CORE_ADDR pc;
  497. {
  498.   register struct block *b = block_for_pc (pc);
  499.   if (b == 0)
  500.     return 0;
  501.   return block_function (b);
  502. }
  503.  
  504. /* These variables are used to cache the most recent result
  505.  * of find_pc_partial_function. */
  506.  
  507. static CORE_ADDR cache_pc_function_low = 0;
  508. static CORE_ADDR cache_pc_function_high = 0;
  509. static char *cache_pc_function_name = 0;
  510.  
  511. /* Clear cache, e.g. when symbol table is discarded. */
  512.  
  513. void
  514. clear_pc_function_cache()
  515. {
  516.   cache_pc_function_low = 0;
  517.   cache_pc_function_high = 0;
  518.   cache_pc_function_name = (char *)0;
  519. }
  520.  
  521. /* Finds the "function" (text symbol) that is smaller than PC
  522.    but greatest of all of the potential text symbols.  Sets
  523.    *NAME and/or *ADDRESS conditionally if that pointer is non-zero.
  524.    Returns 0 if it couldn't find anything, 1 if it did.  On a zero
  525.    return, *NAME and *ADDRESS are always set to zero.  On a 1 return,
  526.    *NAME and *ADDRESS contain real information.  */
  527.  
  528. int
  529. find_pc_partial_function (pc, name, address)
  530.      CORE_ADDR pc;
  531.      char **name;
  532.      CORE_ADDR *address;
  533. {
  534.   struct partial_symtab *pst;
  535.   struct symbol *f;
  536.   struct minimal_symbol *msymbol;
  537.   struct partial_symbol *psb;
  538.  
  539.   if (pc >= cache_pc_function_low && pc < cache_pc_function_high)
  540.     {
  541.     if (address)
  542.         *address = cache_pc_function_low;
  543.     if (name)
  544.         *name = cache_pc_function_name;
  545.     return 1;
  546.     }
  547.  
  548.   pst = find_pc_psymtab (pc);
  549.   if (pst)
  550.     {
  551.       if (pst->readin)
  552.     {
  553.       /* The information we want has already been read in.
  554.          We can go to the already readin symbols and we'll get
  555.          the best possible answer.  */
  556.       f = find_pc_function (pc);
  557.       if (!f)
  558.         {
  559.         return_error:
  560.           /* No available symbol.  */
  561.           if (name != 0)
  562.         *name = 0;
  563.           if (address != 0)
  564.         *address = 0;
  565.           return 0;
  566.         }
  567.  
  568.       cache_pc_function_low = BLOCK_START (SYMBOL_BLOCK_VALUE (f));
  569.       cache_pc_function_high = BLOCK_END (SYMBOL_BLOCK_VALUE (f));
  570.       cache_pc_function_name = SYMBOL_NAME (f);
  571.       if (name)
  572.         *name = cache_pc_function_name;
  573.       if (address)
  574.         *address = cache_pc_function_low;
  575.       return 1;
  576.     }
  577.  
  578.       /* Get the information from a combination of the pst
  579.      (static symbols), and the minimal symbol table (extern
  580.      symbols).  */
  581.       msymbol = lookup_minimal_symbol_by_pc (pc);
  582.       psb = find_pc_psymbol (pst, pc);
  583.  
  584.       if (!psb && (msymbol == NULL))
  585.     {
  586.       goto return_error;
  587.     }
  588.       if (psb
  589.       && (msymbol == NULL
  590.           || (SYMBOL_VALUE_ADDRESS (psb)
  591.           >= msymbol -> address)))
  592.     {
  593.       /* This case isn't being cached currently. */
  594.       if (address)
  595.         *address = SYMBOL_VALUE_ADDRESS (psb);
  596.       if (name)
  597.         *name = SYMBOL_NAME (psb);
  598.       return 1;
  599.     }
  600.     }
  601.   else
  602.     /* Must be in the minimal symbol table.  */
  603.     {
  604.       msymbol = lookup_minimal_symbol_by_pc (pc);
  605.       if (msymbol == NULL)
  606.     goto return_error;
  607.     }
  608.  
  609.   {
  610.     if (msymbol -> type == mst_text)
  611.       cache_pc_function_low = msymbol -> address;
  612.     else
  613.       /* It is a transfer table for Sun shared libraries.  */
  614.       cache_pc_function_low = pc - FUNCTION_START_OFFSET;
  615.   }
  616.   cache_pc_function_name = msymbol -> name;
  617.   /* FIXME:  Deal with bumping into end of minimal symbols for a given
  618.      objfile, and what about testing for mst_text again? */
  619.   if ((msymbol + 1) -> name != NULL)
  620.     cache_pc_function_high = (msymbol + 1) -> address;
  621.   else
  622.     cache_pc_function_high = cache_pc_function_low + 1;
  623.   if (address)
  624.     *address = cache_pc_function_low;
  625.   if (name)
  626.     *name = cache_pc_function_name;
  627.   return 1;
  628. }
  629.  
  630. /* Return the innermost stack frame executing inside of the specified block,
  631.    or zero if there is no such frame.  */
  632.  
  633. #if 0    /* Currently unused */
  634.  
  635. FRAME
  636. block_innermost_frame (block)
  637.      struct block *block;
  638. {
  639.   struct frame_info *fi;
  640.   register FRAME frame;
  641.   register CORE_ADDR start = BLOCK_START (block);
  642.   register CORE_ADDR end = BLOCK_END (block);
  643.  
  644.   frame = 0;
  645.   while (1)
  646.     {
  647.       frame = get_prev_frame (frame);
  648.       if (frame == 0)
  649.     return 0;
  650.       fi = get_frame_info (frame);
  651.       if (fi->pc >= start && fi->pc < end)
  652.     return frame;
  653.     }
  654. }
  655.  
  656. #endif    /* 0 */
  657.  
  658. void
  659. _initialize_blockframe ()
  660. {
  661.   obstack_init (&frame_cache_obstack);
  662. }
  663.